
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author thien
 */
public class ImageBuffer {
    Image image;
    Sprite sprite;
    String str;

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public Sprite getSprite() {
        return sprite;
    }

    public void setSprite(Sprite sprite) {
        this.sprite = sprite;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }
    
    
    public ImageBuffer(String imageName,int row,int column,int dx,int dy,String str) throws IOException{
        Image image = Image.createImage(imageName);
        int width = image.getWidth()/column;
        int height = image.getHeight()/row;
        sprite = new Sprite(image,width,height);
        sprite.setPosition(dx, dy);
        if(str!=null){
            this.str = str;
        }
    }
    
    public ImageBuffer(Image image,int row,int column,int dx,int dy,String str) {
        int width = image.getWidth()/column;
        int height = image.getHeight()/row;
        sprite = new Sprite(image,width,height);
        sprite.setPosition(dx, dy);
        if(str!=null){
            this.str = str;
        }
    }

    public ImageBuffer(Image image,int dx,int dy,String str) {
        sprite = new Sprite(image);
        sprite.setPosition(dx, dy);
        if(str!=null){
            this.str = str;
        }
    }
    
       
    public ImageBuffer(Image image,String str){
        this.image = image;
        this.str = str;
        System.out.println("create image "+image+ " name "+str);
    }

    public int getWidth(){
        if(sprite!=null)
            return sprite.getWidth();
        else
            return -1;
    }
    
    public int getHeight(){
        if(sprite!=null)
            return sprite.getHeight();
        else
            return -1;
    }
    
    public int getX(){
        if(sprite!=null)
            return sprite.getX();
        else
            return -1;        
    }
    
    public int getY(){
        if(sprite!=null)
            return sprite.getY();
        else
            return -1;            
    }
    
    public void addLayer(LayerManager layerManager,int index){
        if(index>0)
            layerManager.insert(sprite, index);
        else
            layerManager.append(sprite);
    }
    
    
    public void draw(Graphics g){
        sprite.paint(g);
    }
    
    public void drawText(Graphics g,int color,int height,int viewX,int viewY){
        if(str!=null){

            Designer.drawString(g, str, 0, 100, Designer.MARGIN_CENTER,false,false,color,sprite.getX()-viewX,sprite.getY()+sprite.getHeight()-viewY,sprite.getWidth(),height);
        }
            
    }
    public void drawRec(Graphics g,int color,int viewX,int viewY)
    {
      g.fillRect(sprite.getX()-viewX, sprite.getY()-viewY-30,sprite.getWidth(), sprite.getHeight());
    }
    public void nextFrame(){
        if(sprite!=null)
            sprite.nextFrame();
    }
    
    public void preFrame(){
        if(sprite!=null)
            sprite.prevFrame();
    }
    
    public int getFrame(){
        if(sprite!=null)
            return sprite.getFrame();
        else
            return -1;
    }
    
    public void setFrame(int index){
        if(sprite!=null)
            sprite.setFrame(index);
    }
}
